home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 October: Mac OS SDK / Dev.CD Oct 97 SDK1.toast / Development Kits (Disc 1) / Interfaces&Libraries / Universal / Interfaces / CIncludes / Threads.h < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-12  |  10.0 KB  |  286 lines  |  [TEXT/MPS ]

  1. /*
  2.      File:        Threads.h
  3.  
  4.      Contains:    Thread Manager Interfaces.
  5.  
  6.      Version:    Technology:    System 7.5
  7.                  Release:    Universal Interfaces 3.0.1
  8.  
  9.      Copyright:    © 1991-1997 by Apple Computer, Inc., all rights reserved.
  10.  
  11.      Bugs?:        Please include the the file and version information (from above) with
  12.                  the problem description.  Developers belonging to one of the Apple
  13.                  developer programs can submit bug reports to:
  14.  
  15.                      devsupport@apple.com
  16.  
  17. */
  18. #ifndef __THREADS__
  19. #define __THREADS__
  20.  
  21. #ifndef __TYPES__
  22. #include <Types.h>
  23. #endif
  24. #ifndef __MIXEDMODE__
  25. #include <MixedMode.h>
  26. #endif
  27. #ifndef __ERRORS__
  28. #include <Errors.h>
  29. #endif
  30.  
  31.  
  32.  
  33. #if PRAGMA_ONCE
  34. #pragma once
  35. #endif
  36.  
  37. #ifdef __cplusplus
  38. extern "C" {
  39. #endif
  40.  
  41. #if PRAGMA_IMPORT
  42. #pragma import on
  43. #endif
  44.  
  45. #if PRAGMA_STRUCT_ALIGN
  46.     #pragma options align=mac68k
  47. #elif PRAGMA_STRUCT_PACKPUSH
  48.     #pragma pack(push, 2)
  49. #elif PRAGMA_STRUCT_PACK
  50.     #pragma pack(2)
  51. #endif
  52.  
  53. /* Thread states */
  54. typedef unsigned short                     ThreadState;
  55.  
  56. enum {
  57.     kReadyThreadState            = 0,
  58.     kStoppedThreadState            = 1,
  59.     kRunningThreadState            = 2
  60. };
  61.  
  62. /* Error codes have been meoved to Errors.(pah) */
  63. /* Thread environment characteristics */
  64. typedef void *                            ThreadTaskRef;
  65. /* Thread characteristics */
  66. typedef unsigned long                     ThreadStyle;
  67.  
  68. enum {
  69.     kCooperativeThread            = 1L << 0,
  70.     kPreemptiveThread            = 1L << 1
  71. };
  72.  
  73. /* Thread identifiers */
  74. typedef unsigned long                     ThreadID;
  75.  
  76. enum {
  77.     kNoThreadID                    = 0,
  78.     kCurrentThreadID            = 1,
  79.     kApplicationThreadID        = 2
  80. };
  81.  
  82. /* Options when creating a thread */
  83. typedef unsigned long                     ThreadOptions;
  84.  
  85. enum {
  86.     kNewSuspend                    = (1 << 0),
  87.     kUsePremadeThread            = (1 << 1),
  88.     kCreateIfNeeded                = (1 << 2),
  89.     kFPUNotNeeded                = (1 << 3),
  90.     kExactMatchThread            = (1 << 4)
  91. };
  92.  
  93. /* Information supplied to the custom scheduler */
  94. struct SchedulerInfoRec {
  95.     unsigned long                     InfoRecSize;
  96.     ThreadID                         CurrentThreadID;
  97.     ThreadID                         SuggestedThreadID;
  98.     ThreadID                         InterruptedCoopThreadID;
  99. };
  100. typedef struct SchedulerInfoRec SchedulerInfoRec;
  101.  
  102. typedef SchedulerInfoRec *                SchedulerInfoRecPtr;
  103.  
  104. #if TARGET_CPU_68K && TARGET_RT_MAC_CFM
  105. /*
  106.     The following UniversalProcPtrs are for CFM-68k compatiblity with
  107.     the implementation of the Thread Manager.
  108. */
  109. typedef UniversalProcPtr                 ThreadEntryProcPtr;
  110. typedef UniversalProcPtr                 ThreadSchedulerProcPtr;
  111. typedef UniversalProcPtr                 ThreadSwitchProcPtr;
  112. typedef UniversalProcPtr                 ThreadTerminationProcPtr;
  113. typedef UniversalProcPtr                 DebuggerNewThreadProcPtr;
  114. typedef UniversalProcPtr                 DebuggerDisposeThreadProcPtr;
  115. typedef UniversalProcPtr                 DebuggerThreadSchedulerProcPtr;
  116. enum {
  117.     uppThreadEntryProcInfo = kPascalStackBased
  118.          | RESULT_SIZE(SIZE_CODE(sizeof(void*)))
  119.          | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(void*))),
  120.     uppThreadSchedulerProcInfo = kPascalStackBased
  121.          | RESULT_SIZE(SIZE_CODE(sizeof(ThreadID)))
  122.          | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(SchedulerInfoRecPtr))),
  123.     uppThreadSwitchProcInfo = kPascalStackBased
  124.          | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(ThreadID)))
  125.          | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(void*))),
  126.     uppThreadTerminationProcInfo = kPascalStackBased
  127.          | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(ThreadID)))
  128.          | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(void*))),
  129.     uppDebuggerNewThreadProcInfo = kPascalStackBased
  130.          | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(ThreadID))),
  131.     uppDebuggerDisposeThreadProcInfo = kPascalStackBased
  132.          | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(ThreadID))),
  133.     uppDebuggerThreadSchedulerProcInfo = kPascalStackBased
  134.          | RESULT_SIZE(SIZE_CODE(sizeof(ThreadID)))
  135.          | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(SchedulerInfoRecPtr)))
  136. };
  137. #define NewThreadEntryProc(userRoutine)                (ThreadEntryProcPtr) NewRoutineDescriptor((ProcPtr)(userRoutine), uppThreadEntryProcInfo, GetCurrentArchitecture())
  138. #define NewThreadSchedulerProc(userRoutine)            (ThreadSchedulerProcPtr) NewRoutineDescriptor((ProcPtr)(userRoutine), uppThreadSchedulerProcInfo, GetCurrentArchitecture())
  139. #define NewThreadSwitchProc(userRoutine)            (ThreadSwitchProcPtr) NewRoutineDescriptor((ProcPtr)(userRoutine), uppThreadSwitchProcInfo, GetCurrentArchitecture())
  140. #define NewThreadTerminationProc(userRoutine)        (ThreadTerminationProcPtr) NewRoutineDescriptor((ProcPtr)(userRoutine), uppThreadTerminationProcInfo, GetCurrentArchitecture())
  141. #define NewDebuggerNewThreadProc(userRoutine)        (DebuggerNewThreadProcPtr) NewRoutineDescriptor((ProcPtr)(userRoutine), uppDebuggerNewThreadProcInfo, GetCurrentArchitecture())
  142. #define NewDebuggerDisposeThreadProc(userRoutine)    (DebuggerDisposeThreadProcPtr) NewRoutineDescriptor((ProcPtr)(userRoutine), uppDebuggerDisposeThreadProcInfo, GetCurrentArchitecture())
  143. #define NewDebuggerThreadSchedulerProc(userRoutine)    (DebuggerThreadSchedulerProcPtr) NewRoutineDescriptor((ProcPtr)(userRoutine), uppDebuggerThreadSchedulerProcInfo, GetCurrentArchitecture())
  144. #else
  145. /*
  146.     The following ProcPtrs cannot be interchanged with UniversalProcPtrs because
  147.     of differences between 680x0 and PowerPC runtime architectures with regard to
  148.     the implementation of the Thread Manager.
  149.  */
  150. /* Prototype for thread's entry (main) routine */
  151. typedef void *                            voidPtr;
  152. typedef CALLBACK_API( voidPtr , ThreadEntryProcPtr )(void *threadParam);
  153. /* Prototype for custom thread scheduler routine */
  154. typedef CALLBACK_API( ThreadID , ThreadSchedulerProcPtr )(SchedulerInfoRecPtr schedulerInfo);
  155. /* Prototype for custom thread switcher routine */
  156. typedef CALLBACK_API( void , ThreadSwitchProcPtr )(ThreadID threadBeingSwitched, void *switchProcParam);
  157. /* Prototype for thread termination notification routine */
  158. typedef CALLBACK_API( void , ThreadTerminationProcPtr )(ThreadID threadTerminated, void *terminationProcParam);
  159. /* Prototype for debugger NewThread notification */
  160. typedef CALLBACK_API( void , DebuggerNewThreadProcPtr )(ThreadID threadCreated);
  161. /* Prototype for debugger DisposeThread notification */
  162. typedef CALLBACK_API( void , DebuggerDisposeThreadProcPtr )(ThreadID threadDeleted);
  163. /* Prototype for debugger schedule notification */
  164. typedef CALLBACK_API( ThreadID , DebuggerThreadSchedulerProcPtr )(SchedulerInfoRecPtr schedulerInfo);
  165. #endif  /* TARGET_CPU_68K && TARGET_RT_MAC_CFM */
  166.  
  167.  
  168. /* Thread Manager routines */
  169. EXTERN_API( OSErr )
  170. CreateThreadPool                (ThreadStyle             threadStyle,
  171.                                  short                     numToCreate,
  172.                                  Size                     stackSize)                            THREEWORDINLINE(0x303C, 0x0501, 0xABF2);
  173.  
  174. EXTERN_API( OSErr )
  175. GetFreeThreadCount                (ThreadStyle             threadStyle,
  176.                                  short *                freeCount)                            THREEWORDINLINE(0x303C, 0x0402, 0xABF2);
  177.  
  178. EXTERN_API( OSErr )
  179. GetSpecificFreeThreadCount        (ThreadStyle             threadStyle,
  180.                                  Size                     stackSize,
  181.                                  short *                freeCount)                            THREEWORDINLINE(0x303C, 0x0615, 0xABF2);
  182.  
  183. EXTERN_API( OSErr )
  184. GetDefaultThreadStackSize        (ThreadStyle             threadStyle,
  185.                                  Size *                    stackSize)                            THREEWORDINLINE(0x303C, 0x0413, 0xABF2);
  186.  
  187. EXTERN_API( OSErr )
  188. ThreadCurrentStackSpace            (ThreadID                 thread,
  189.                                  unsigned long *        freeStack)                            THREEWORDINLINE(0x303C, 0x0414, 0xABF2);
  190.  
  191. EXTERN_API( OSErr )
  192. NewThread                        (ThreadStyle             threadStyle,
  193.                                  ThreadEntryProcPtr     threadEntry,
  194.                                  void *                    threadParam,
  195.                                  Size                     stackSize,
  196.                                  ThreadOptions             options,
  197.                                  void **                threadResult,
  198.                                  ThreadID *                threadMade)                            THREEWORDINLINE(0x303C, 0x0E03, 0xABF2);
  199.  
  200. EXTERN_API( OSErr )
  201. DisposeThread                    (ThreadID                 threadToDump,
  202.                                  void *                    threadResult,
  203.                                  Boolean                 recycleThread)                        THREEWORDINLINE(0x303C, 0x0504, 0xABF2);
  204.  
  205. EXTERN_API( OSErr )
  206. YieldToThread                    (ThreadID                 suggestedThread)                    THREEWORDINLINE(0x303C, 0x0205, 0xABF2);
  207.  
  208. EXTERN_API( OSErr )
  209. YieldToAnyThread                (void)                                                        FOURWORDINLINE(0x42A7, 0x303C, 0x0205, 0xABF2);
  210.  
  211. EXTERN_API( OSErr )
  212. GetCurrentThread                (ThreadID *                currentThreadID)                    THREEWORDINLINE(0x303C, 0x0206, 0xABF2);
  213.  
  214. EXTERN_API( OSErr )
  215. GetThreadState                    (ThreadID                 threadToGet,
  216.                                  ThreadState *            threadState)                        THREEWORDINLINE(0x303C, 0x0407, 0xABF2);
  217.  
  218. EXTERN_API( OSErr )
  219. SetThreadState                    (ThreadID                 threadToSet,
  220.                                  ThreadState             newState,
  221.                                  ThreadID                 suggestedThread)                    THREEWORDINLINE(0x303C, 0x0508, 0xABF2);
  222.  
  223. EXTERN_API( OSErr )
  224. SetThreadStateEndCritical        (ThreadID                 threadToSet,
  225.                                  ThreadState             newState,
  226.                                  ThreadID                 suggestedThread)                    THREEWORDINLINE(0x303C, 0x0512, 0xABF2);
  227.  
  228. EXTERN_API( OSErr )
  229. SetThreadScheduler                (ThreadSchedulerProcPtr  threadScheduler)                    THREEWORDINLINE(0x303C, 0x0209, 0xABF2);
  230.  
  231. EXTERN_API( OSErr )
  232. SetThreadSwitcher                (ThreadID                 thread,
  233.                                  ThreadSwitchProcPtr     threadSwitcher,
  234.                                  void *                    switchProcParam,
  235.                                  Boolean                 inOrOut)                            THREEWORDINLINE(0x303C, 0x070A, 0xABF2);
  236.  
  237. EXTERN_API( OSErr )
  238. SetThreadTerminator                (ThreadID                 thread,
  239.                                  ThreadTerminationProcPtr  threadTerminator,
  240.                                  void *                    terminationProcParam)                THREEWORDINLINE(0x303C, 0x0611, 0xABF2);
  241.  
  242. EXTERN_API( OSErr )
  243. ThreadBeginCritical                (void)                                                        THREEWORDINLINE(0x303C, 0x000B, 0xABF2);
  244.  
  245. EXTERN_API( OSErr )
  246. ThreadEndCritical                (void)                                                        THREEWORDINLINE(0x303C, 0x000C, 0xABF2);
  247.  
  248. EXTERN_API( OSErr )
  249. SetDebuggerNotificationProcs    (DebuggerNewThreadProcPtr  notifyNewThread,
  250.                                  DebuggerDisposeThreadProcPtr  notifyDisposeThread,
  251.                                  DebuggerThreadSchedulerProcPtr  notifyThreadScheduler)        THREEWORDINLINE(0x303C, 0x060D, 0xABF2);
  252.  
  253. EXTERN_API( OSErr )
  254. GetThreadCurrentTaskRef            (ThreadTaskRef *        threadTRef)                            THREEWORDINLINE(0x303C, 0x020E, 0xABF2);
  255.  
  256. EXTERN_API( OSErr )
  257. GetThreadStateGivenTaskRef        (ThreadTaskRef             threadTRef,
  258.                                  ThreadID                 threadToGet,
  259.                                  ThreadState *            threadState)                        THREEWORDINLINE(0x303C, 0x060F, 0xABF2);
  260.  
  261. EXTERN_API( OSErr )
  262. SetThreadReadyGivenTaskRef        (ThreadTaskRef             threadTRef,
  263.                                  ThreadID                 threadToSet)                        THREEWORDINLINE(0x303C, 0x0410, 0xABF2);
  264.  
  265.  
  266. #if PRAGMA_STRUCT_ALIGN
  267.     #pragma options align=reset
  268. #elif PRAGMA_STRUCT_PACKPUSH
  269.     #pragma pack(pop)
  270. #elif PRAGMA_STRUCT_PACK
  271.     #pragma pack()
  272. #endif
  273.  
  274. #ifdef PRAGMA_IMPORT_OFF
  275. #pragma import off
  276. #elif PRAGMA_IMPORT
  277. #pragma import reset
  278. #endif
  279.  
  280. #ifdef __cplusplus
  281. }
  282. #endif
  283.  
  284. #endif /* __THREADS__ */
  285.  
  286.